package com.android.thought.badboys;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import com.android.thought.badboys.Action.IBackupAccessAction;
import com.android.thought.badboys.Action.IPhoneAccessAction;
import com.android.thought.badboys.Action.ISmsAction;
import com.android.thought.badboys.Call.CallActivity;
import com.android.thought.badboys.Common.Const;
import com.android.thought.badboys.Info.ListBackup;
import com.android.thought.badboys.Info.SelectionPhoneInfo;
import com.android.thought.badboys.PhoneBook.PhoneBookActvity;
import com.android.thought.badboys.Setting.SettingActivity;
import com.android.thought.badboys.Sms.SmsActivity;

public class MainActivity extends Activity implements OnClickListener {

	
	Model mModel = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		startService(new Intent(this, Services.class));
		
		Application app = getApplication();
		
		if ( app instanceof BadBoysApplication ) { 
			mModel= ((BadBoysApplication)app).getModel();
		}
		
		setContentView(R.layout.activity_main);

		
		mModel.getAdministrationPhoneInfo(this, new IPhoneAccessAction() {
			
			@Override
			public void onSaveComplated(boolean isSucess) {
				
			}
			
			@Override
			public void onLoadComplated(SelectionPhoneInfo info) {
				mModel.getBackUpInfo(MainActivity.this, new IBackupAccessAction() {
					
					@Override
					public void onSaveComplated(boolean isSucess) {
					}
					
					@Override
					public void onLoadComplated(ListBackup backup) {
						onComplteUiUpdate();
					}
				}, true);
			}
		});
	}
	
	private void onComplteUiUpdate() { 

		this.findViewById(R.id.btn_login).setOnClickListener(this);
		this.findViewById(R.id.btn_cash_p).setOnClickListener(this);
		this.findViewById(R.id.btn_sms).setOnClickListener(this);
		this.findViewById(R.id.btn_setting).setOnClickListener(this);
		this.findViewById(R.id.btn_call).setOnClickListener(this);
		this.findViewById(R.id.btn_phone).setOnClickListener(this);
		
		this.findViewById(R.id.btn_temp_delete).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		final int id = v.getId();
		final Intent intent;
		if (id == R.id.btn_phone) {
			intent = new Intent(this, PhoneBookActvity.class);
			startActivityForResult(intent, Const.REQUEST_ACTIVTY_PHONEBOOK);
		} else if (id == R.id.btn_call) {
			intent = new Intent(this, CallActivity.class);
			startActivityForResult(intent, Const.REQUEST_ACTIVTY_CALL);
		} else if (id == R.id.btn_sms) {
			intent = new Intent(this, SmsActivity.class);
			startActivityForResult(intent, Const.REQUEST_ACTIVTY_SMS);
		} else if (id == R.id.btn_sns) {

		} else if (id == R.id.btn_setting) {
			intent = new Intent(this, SettingActivity.class);
			startActivityForResult(intent, Const.REQUEST_ACTIVTY_SETTING);
			

		} else if ( id == R.id.btn_temp_delete ) {
			
			mModel.onDeleteMessageDelete(this, new ISmsAction() {
				
				@Override
				public void onLoadComplated(Boolean returnValue) {

					if( returnValue && returnValue != null ) {
						mModel.onSaveBackUpData(MainActivity.this, new IBackupAccessAction() {
							
							@Override
							public void onSaveComplated(boolean isSucess) {
								
								Log.e("CH_DEBUG", "#### SMS __세이브");
							}
							
							@Override
							public void onLoadComplated(ListBackup backup) {
								Log.e("CH_DEBUG", "#### SMS __로딩완료");
							}
						});
					}
					
				}
			});
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if( requestCode == Const.REQUEST_ACTIVTY_PHONEBOOK ) {
			if ( mModel != null)
				mModel.onSaveAdministrationPhoneInfo(this, null);
			
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

}
